<?php
namespace app\index\service;

use app\index\model\BidModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiModel;
use app\index\model\CossModel;
use app\index\model\DeptModel;
use app\index\model\DossModel;
use app\index\model\EnginModel;
use app\index\model\EnginShuiModel;
use app\index\model\HouGonghuiModel;
use app\index\model\HouGongshuiModel;
use app\index\model\HouGongyuModel;
use app\index\model\HouGongzhiGuanModel;
use app\index\model\HouGongzhiListModel;
use app\index\model\HouGongzhiModel;
use app\index\model\HouGongzhiQuModel;
use app\index\model\HouJieModel;
use app\index\model\HouKouFuwuModel;
use app\index\model\HouKouGongjieModel;
use app\index\model\HouKouKuaifeiModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\HouTiaoModel;
use app\index\model\KuaiJiModel;
use app\index\model\KuaiShouModel;
use app\index\model\QianPiaoModel;
use app\index\model\XossModel;
use think\Db;

class HouService extends CommonService
{
    public function check_doss_number($number, $id=0){
        return !!DossModel::where(['comid'=>$this->userinfo['comid'],'number'=>$number,'deleted_user'=>0])
            ->where('id','neq', $id)->find();
    }

    public function check_coss_number($number, $id=0){
        return !!CossModel::where(['comid'=>$this->userinfo['comid'],'number'=>$number,'deleted_user'=>0])
            ->where('id','neq', $id)->find();
    }

    public function check_xoss_number($number, $id=0){
        return !!XossModel::where(['comid'=>$this->userinfo['comid'],'number'=>$number,'deleted_user'=>0])
            ->where('id','neq', $id)->find();
    }
    public function get_hou_gongzhi_fulei(){
        return ['工程款项支付', '仅材料款支付'];
    }
    public function get_hou_chushui_yiju(){
        return [
            1 => '按付款金额',
            2 => '按收票金额',
        ];
    }
    public function get_hou_chushui_yiju_name($yiju){
        $arr = $this->get_hou_chushui_yiju();
        $str = isset($arr[$yiju]) ? $arr[$yiju] : '';
        return $str;
    }

    public function get_hou_gongshui_tuifang(){
        return [
            1 => '直接支付',
            2 => '转为工程负责人预收款',
        ];
    }
    public function get_hou_gongshui_tuifang_name($tuifang){
        $arr = $this->get_hou_gongshui_tuifang();
        $str = isset($arr[$tuifang]) ? $arr[$tuifang] : '';
        return $str;
    }




    public function get_hou_koulei(){
        return [
            'fuwu' => '待收取劳务分包服务费',
            'shuifei' => '待收取税款(含利息)',
            'gongjie' => '待收取工程负责人借款(含利息)',
            'kuaifei' => '待收取快递费',
            'qifei' => '直接收取',
        ];
    }
    public function get_hou_koulei_name($koulei){
        $str = '';
        $arr = $this->get_hou_koulei();
        $kouleiArr = explode(',', $koulei);
        foreach ($arr as $kkk=>$vvv){
            if(in_array($kkk,$kouleiArr)){
                if($str){
                    $str .= '，'.$vvv;
                }else{
                    $str .= $vvv;
                }
            }
        }
        return $str;
    }







    //采购合同金额
    public function get_contract_cai_money($enginid, $gonger=''){
        $whereGonger = [];
        if($gonger){
            $whereGonger[] = ['gonger','=',$gonger];
        }
        $money = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
            ->where(['enginid'=>$enginid])->where($whereGonger)->sum('money');
        return $money;
    }
    //采购合同金额 - 有结算得优先结算金额
    public function get_contract_cai_final_money($enginid, $gonger=''){
        $money = 0;

        $whereGonger = [];
        if($gonger){
            $whereGonger[] = ['gonger','=',$gonger];
        }
        $caiinfo = ContractCaiModel::field('id,money')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
            ->where(['enginid'=>$enginid])->where($whereGonger)->select()->toArray();
        foreach ($caiinfo as $item){
            $jieMoney = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
                ->where(['enginid'=>$enginid,'caiid'=>$item['id']])->sum('money');
            if($jieMoney){
                $money += $jieMoney;
            }else{
                $money += $item['money'];
            }
        }
        return $money;
    }


    //累计工程回款 取值主信息字段【到账金额】
    public function get_hou_gonghui_money($enginid=0){
        $money = HouGonghuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('money');
        return $money;
    }
    //累计预收款 取值主信息字段【到账金额】
    public function get_hou_gongyu_money($gofuer=''){
        $money = HouGongyuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'gofuer'=>$gofuer])->sum('money');
        return $money;
    }
    //累计已收取 1、取工程汇款 取值主信息字段【本次收取合计】
    public function get_hou_gonghui_koufee($enginid=0){
        $money = HouGonghuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('koufee');
        return $money;
    }
    //累计已收取 2、取工程负责人预收款  取值主信息字段【本次收取合计】
    public function get_hou_gongyu_koufee($gofuer=''){
        $money = HouGongyuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'gofuer'=>$gofuer])->sum('koufee');
        return $money;
    }
    //累计已收取 3、取工程负责人预收款  取值主信息字段【本次收取合计】
    public function get_hou_gong_koufee_xiang($enginid=0){
        //===劳务分包服务费===
        $money = HouKouFuwuModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$enginid])
            ->sum('list.koufee');

        $money += HouKouFuwuModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$enginid])
            ->sum('list.koufee');

        //===快递费===
        $money += HouKouKuaifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->join('kuai_ji kuai','kuai.id=list.kuaiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['kuai.comid'=>$this->userinfo['comid'],'kuai.deleted_user'=>0,'kuai.isturn'=>1,'kuai.status'=>1])
            ->where(['list.type'=>1,'list.kuaitype'=>1, 'kuai.enginid'=>$enginid])
            ->sum('list.koufee');

        $money += HouKouKuaifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->join('kuai_shou kuai','kuai.id=list.kuaiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['kuai.comid'=>$this->userinfo['comid'],'kuai.deleted_user'=>0,'kuai.isturn'=>1,'kuai.status'=>1])
            ->where(['list.type'=>1,'list.kuaitype'=>2, 'kuai.enginid'=>$enginid])
            ->sum('list.koufee');

        $money += HouKouKuaifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('kuai_ji kuai','kuai.id=list.kuaiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['kuai.comid'=>$this->userinfo['comid'],'kuai.deleted_user'=>0,'kuai.isturn'=>1,'kuai.status'=>1])
            ->where(['list.type'=>2,'list.kuaitype'=>1, 'kuai.enginid'=>$enginid])
            ->sum('list.koufee');

        $money += HouKouKuaifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('kuai_shou kuai','kuai.id=list.kuaiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['kuai.comid'=>$this->userinfo['comid'],'kuai.deleted_user'=>0,'kuai.isturn'=>1,'kuai.status'=>1])
            ->where(['list.type'=>2,'list.kuaitype'=>2, 'kuai.enginid'=>$enginid])
            ->sum('list.koufee');

        //===税款===
        $money += HouKouShuifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
            ->where(['list.type'=>1, 'shui.enginid'=>$enginid])
            ->sum('list.koufee');

        $money += HouKouShuifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
            ->where(['list.type'=>2, 'shui.enginid'=>$enginid])
            ->sum('list.koufee');

        //===直接收取===
        $money += HouKouQifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$enginid])
            ->sum('list.koufee');

        $money += HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$enginid])
            ->sum('list.koufee');

        return $money;
    }




    //累计工程付款 1、取值主信息字段【本次付款合计】
    public function get_hou_gongzhi_money($enginid=0){
        $money = HouGongzhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$enginid])->sum('money');
        return $money;
    }
    //累计工程付款 2、取值明细->资金渠道->资金渠道是本项目的金额合计
    public function get_hou_gongzhi_money_quben($enginid=0){
        $money = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['qu.type'=>2, 'zhi.enginid'=>$enginid])
            ->sum('qu.money');

        $money += HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['qu.type'=>3, 'qu.qienid'=>$enginid])
            ->sum('qu.money');

        return $money;
    }
    //累计工程付款 3、取值明细->资金渠道->资金渠道是工程负责人预收款的金额合计
    public function get_hou_gongzhi_money_qugoer($gofuer='', $enginid=0){
        $whereEngin = [];
        if($enginid){
            $whereEngin[] = ['id','=',$enginid];
        }

        $enginidArr =  EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'gofuer'=>$gofuer])->where($whereEngin)->column('id');
        $money = HouGongzhiQuModel::alias('qu')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['qu.type'=>1])
            ->where('zhi.enginid', 'in', $enginidArr)
            ->sum('qu.money');
        return $money;
    }
    //工程累计支付销售方 4、取值明细->本次付款金额  付款对象所属的往来单位类别=销售方
    public function get_hou_gongzhi_money_gogong($enginid=0){
        $money = HouGongzhiListModel::alias('list')
            ->join('hou_gongzhi zhi','zhi.id=list.gongzhiid')
            ->join('dealcom deal','deal.name=list.fuer')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['zhi.enginid'=>$enginid,'deal.type'=>3])
            ->sum('list.money');
        return $money;
    }



    //进项抵扣退税转入  单个项目 取值进项抵扣退税(退税方式=转为工程负责人预收款)信息字段【本次进项抵扣退税】
    public function get_hou_gongshui_money($enginid=0){
        $money = HouGongshuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$enginid, 'tuifang'=>2])->sum('money');
        return $money;
    }
    //进项抵扣退税转入  销售方下所有项目 取值进项抵扣退税(退税方式=转为工程负责人预收款)信息字段【本次进项抵扣退税】
    public function get_hou_gongshui_money_goquan($gofuer=''){
        $enginidArr =  EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'gofuer'=>$gofuer])->column('id');
        $money = HouGongshuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'tuifang'=>2])
            ->where('enginid', 'in', $enginidArr)
            ->sum('money');
        return $money;
    }





















    //获取工程最近一次收取明细信息【审核通过 到账日期最大的收取数据】
    public function get_engin_last_kou_list($enginid){
        $list = [];
        $huiinfo = HouGonghuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$enginid])->order('dt DESC')->find();
        if(!$huiinfo){
            return $list;
        }


        //劳务分包服务费
        $fee_fuwu = HouKouFuwuModel::field('enginid,koufee,shoubiao,explain')
            ->where(['type'=>1,'zhuid'=>$huiinfo['id']])->select()->toArray();
        foreach ($fee_fuwu as $vv_fuwu){
            $vv_fuwu['houkoulei'] = 1;
            $vv_fuwu['name'] = '劳务分包服务费';
            $vv_fuwu['enginname'] = EnginModel::where(['id'=>$vv_fuwu['enginid']])->value('name');
            $list[] = $vv_fuwu;
        }
        //税款保证金
        $fee_shuifei = HouKouShuifeiModel::field('shuiid,koufee,shoubiao,explain')
            ->where(['type'=>1,'zhuid'=>$huiinfo['id'],'houkoulei'=>2])->select()->toArray();
        foreach ($fee_shuifei as $vv_shuifei){
            $vv_shuifei['houkoulei'] = 2;
            $vv_shuifei['name'] = '税款保证金';
            $shuiinfo = EnginShuiModel::where(['id'=>$vv_shuifei['shuiid']])->find();
            $vv_shuifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
            $list[] = $vv_shuifei;
        }
        //税款利息
        $fee_shuifei_lixi = HouKouShuifeiModel::field('shuiid,koufee,shoubiao,explain')
            ->where(['type'=>1,'zhuid'=>$huiinfo['id'],'houkoulei'=>3])->select()->toArray();
        foreach ($fee_shuifei_lixi as $vv_shuifei_lixi){
            $vv_shuifei_lixi['houkoulei'] = 3;
            $vv_shuifei_lixi['name'] = '税款保证金利息';
            $shuiinfo = EnginShuiModel::where(['id'=>$vv_shuifei_lixi['shuiid']])->find();
            $vv_shuifei_lixi['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
            $list[] = $vv_shuifei_lixi;
        }
        //工程负责人借款
        $fee_gongjie = HouKouGongjieModel::field('koufee,shoubiao,explain')
            ->where(['type'=>1,'zhuid'=>$huiinfo['id']])->select()->toArray();
        foreach ($fee_gongjie as $vv_gongjie){
            $vv_gongjie['houkoulei'] = 5;
            $vv_gongjie['name'] = '借款本金及利息';
            $vv_gongjie['enginname'] = '';
            $list[] = $vv_gongjie;
        }
        //快递费
        $fee_kuaifee = HouKouKuaifeiModel::field('kuaitype,kuaiid,koufee,shoubiao,explain')
            ->where(['type'=>1,'zhuid'=>$huiinfo['id']])->select()->toArray();
        foreach ($fee_kuaifee as $vv_kuaifee){
            $vv_kuaifee['houkoulei'] = 4;
            $vv_kuaifee['name'] = '快递费';
            $vv_kuaifee['enginname'] = '';
            if($vv_kuaifee['kuaitype'] == 1){
                $jiinfo = KuaiJiModel::where(['id'=>$vv_kuaifee['kuaiid']])->find();
                $vv_kuaifee['enginname'] = EnginModel::where(['id'=>$jiinfo['enginid']])->value('name');
            }else{
                $shouinfo = KuaiShouModel::where(['id'=>$vv_kuaifee['kuaiid']])->find();
                $vv_kuaifee['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
            }

            $list[] = $vv_kuaifee;
        }
        //其他费
        $fee_qifee = HouKouQifeiModel::field('enginid,koufee,shoubiao,explain,houkoulei')
            ->where(['type'=>1,'zhuid'=>$huiinfo['id']])->select()->toArray();
        foreach ($fee_qifee as $vv_qi){
            $vv_qi['name'] = (new OptService())->get_base_showname('cai_houkoulei', $vv_qi['houkoulei']);
            $vv_qi['enginname'] = EnginModel::where(['id'=>$vv_qi['enginid']])->value('name');
            $list[] = $vv_qi;
        }
        return $list;
    }




    public function get_jieinfo($data, $daodt=''){
        $huanArr = [];
        $huanArr[] = ['huandt'=>$data['daodt'], 'kouben'=>0, 'kouli'=>0, 'koufee'=>0, 'youfee'=>0];
        if($daodt){
            $huanArr[] = ['huandt'=>$daodt, 'kouben'=>0, 'kouli'=>0, 'koufee'=>0, 'youfee'=>0];
        }else{
            $huanArr[] = ['huandt'=>date("Y-m-d"), 'kouben'=>0, 'kouli'=>0, 'koufee'=>0, 'youfee'=>0];
        }



        $huanHui = HouKouGongjieModel::alias('list')
            ->field('list.*,hou.dt as huandt')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1,'list.jieid'=>$data['id']])
            ->select()->toArray();
        foreach ($huanHui as $huiVV){
            $huanArr[] = $huiVV;
        }

        $huanYu = HouKouGongjieModel::alias('list')
            ->field('list.*,hou.dt as huandt')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2,'list.jieid'=>$data['id']])
            ->select()->toArray();
        foreach ($huanYu as $YuVV){
            $huanArr[] = $YuVV;
        }

        array_multisort(array_column($huanArr,'huandt'),SORT_ASC, $huanArr);


        $data['yueshu_quan'] = 0;
        $data['yueshu_zheng'] = 0;
        $data['yueshu_xiao'] = 0;
        $data['yueshu_zhan'] = 0;

        $data['yinghuanbenfee'] = $data['daomoney'];
        $data['yinghuanlifee'] = 0;
        $data['yinghuanallfee'] = 0;

        $data['yihuanbenfee'] = 0;
        $data['yihuanlifee'] = 0;
        $data['yihuanallfee'] = 0;
        $data['lixiyoufee'] = 0;

        $data['daihuanbenfee'] = 0;
        $data['daihuanlifee'] = 0;
        $data['daihuanallfee'] = 0;

        foreach ($huanArr as $vv){
            $data['yihuanbenfee'] += $vv['kouben'];
            $data['yihuanlifee'] += $vv['kouli'];
            $data['yihuanallfee'] += $vv['koufee'];
            $data['lixiyoufee'] += $vv['youfee'];
        }


        if($data['daodt'] && $data['daodt'] != '0000-00-00'){
            $daihuanben = $data['daomoney'];
            foreach ($huanArr as $k=>$item){
                if(!isset($huanArr[$k+1])){
                    break;
                }
                if($k==0){
                    $yueshu_quan = (( strtotime($huanArr[$k+1]['huandt']) - strtotime($item['huandt']))/86400+1)/30;
                }else{
                    $yueshu_quan = (( strtotime($huanArr[$k+1]['huandt']) - strtotime($item['huandt']))/86400)/30;
                }
                if($yueshu_quan < 0){
                    $yueshu_quan = 0;
                    $yueshu_zheng = 0;
                    $yueshu_xiao = 0;
                    $yueshu_zhan = 0;
                }else{
                    $yueshu_zheng = floor($yueshu_quan);
                    $yueshu_xiao = $yueshu_quan-$yueshu_zheng;

                    if($yueshu_xiao > 0.3){
                        $yueshu_zhan = $yueshu_zheng+1;
                    }else{
                        $yueshu_zhan = round($yueshu_quan, 2);
                    }
                }


                $daihuanben = $daihuanben - $huanArr[$k]['kouben'];
                $lixifee = round($daihuanben*$yueshu_zhan * $data['yuerate'] / 100, 2);

                $data['yueshu_zhan'] += $yueshu_zhan;
                $data['yinghuanlifee'] += $lixifee;
            }
        }


        $data['yinghuanallfee'] = $data['yinghuanbenfee']+$data['yinghuanlifee'];
        $data['yihuanallfee'] = $data['yihuanbenfee']+$data['yihuanlifee'];

        $data['daihuanbenfee'] = $data['yinghuanbenfee'] - $data['yihuanbenfee'];
        $data['daihuanlifee'] = $data['yinghuanlifee'] - $data['yihuanlifee']-$data['lixiyoufee'];
        $data['daihuanallfee'] = $data['daihuanbenfee']+$data['daihuanlifee'];





        $data['yinghuanbenfee'] = round($data['yinghuanbenfee'], 2);
        $data['yinghuanlifee'] = round($data['yinghuanlifee'], 2);
        $data['yinghuanallfee'] = round($data['yinghuanallfee'], 2);

        $data['yihuanbenfee'] = round($data['yihuanbenfee'], 2);
        $data['yihuanlifee'] = round($data['yihuanlifee'], 2);
        $data['yihuanallfee'] = round($data['yihuanallfee'], 2);
        $data['lixiyoufee'] = round($data['lixiyoufee'], 2);

        $data['daihuanbenfee'] = round($data['daihuanbenfee'], 2);
        $data['daihuanlifee'] = round($data['daihuanlifee'], 2);
        $data['daihuanallfee'] = round($data['daihuanallfee'], 2);

        $data['statename'] = '未还款';
        if($data['yihuanallfee'] <= 0){
            $data['statename'] = '未还款';
        }else{
            if($data['daihuanallfee'] <= 0){
                $data['statename'] = '已还款';
            }else{
                $data['statename'] = '部分还款';
            }
        }


        return $data;
    }


    public function get_tiaoinfo($data, $daodt=''){
        $jieinfo = HouJieModel::where(['id'=>$data['jieid']])->find();
        $data['yuerate'] = $jieinfo['yuerate'];
        $data['number'] = $jieinfo['number'];
        $data['chuer'] = $jieinfo['chuer'];


        $huanArr = [];
        $huanArr[] = ['huandt'=>$data['daodt'], 'kouben'=>0, 'kouli'=>0, 'koufee'=>0, 'youfee'=>0];
        if($daodt){
            $huanArr[] = ['huandt'=>$daodt, 'kouben'=>0, 'kouli'=>0, 'koufee'=>0, 'youfee'=>0];
        }else{
            $huanArr[] = ['huandt'=>date("Y-m-d"), 'kouben'=>0, 'kouli'=>0, 'koufee'=>0, 'youfee'=>0];
        }


        //最近还款日期
        $data['zuijindt'] = '';
        $huanHui = HouKouGongjieModel::alias('list')
            ->field('list.*,hou.dt as huandt')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1,'list.tiaoid'=>$data['id']])
            ->select()->toArray();
        foreach ($huanHui as $huiVV){
            $huanArr[] = $huiVV;
            if($huiVV['huandt'] > $data['zuijindt']){
                $data['zuijindt'] = $huiVV['huandt'];
            }
        }

        $huanYu = HouKouGongjieModel::alias('list')
            ->field('list.*,hou.dt as huandt')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2,'list.tiaoid'=>$data['id']])
            ->select()->toArray();
        foreach ($huanYu as $YuVV){
            $huanArr[] = $YuVV;
            if($YuVV['huandt'] > $data['zuijindt']){
                $data['zuijindt'] = $YuVV['huandt'];
            }
        }

        array_multisort(array_column($huanArr,'huandt'),SORT_ASC, $huanArr);


        $data['yueshu_quan'] = 0;
        $data['yueshu_zheng'] = 0;
        $data['yueshu_xiao'] = 0;
        $data['yueshu_zhan'] = 0;

        $data['yinghuanbenfee'] = $data['money'];

        $data['yinghuanlifee'] = 0;
        $data['yinghuanallfee'] = 0;

        $data['yihuanbenfee'] = 0;
        $data['yihuanlifee'] = 0;
        $data['yihuanallfee'] = 0;
        $data['lixiyoufee'] = 0;

        $data['daihuanbenfee'] = 0;
        $data['daihuanlifee'] = 0;
        $data['daihuanallfee'] = 0;

        foreach ($huanArr as $vv){
            $data['yihuanbenfee'] += $vv['kouben'];
            $data['yihuanlifee'] += $vv['kouli'];
            $data['yihuanallfee'] += $vv['koufee'];
            $data['lixiyoufee'] += $vv['youfee'];
        }


        if($data['daodt'] && $data['daodt'] != '0000-00-00'){
            $daihuanben = $data['money'];
            foreach ($huanArr as $k=>$item){
                if(!isset($huanArr[$k+1])){
                    break;
                }
                if($k==0){
                    $yueshu_quan = (( strtotime($huanArr[$k+1]['huandt']) - strtotime($item['huandt']))/86400+1)/30;
                }else{
                    $yueshu_quan = (( strtotime($huanArr[$k+1]['huandt']) - strtotime($item['huandt']))/86400)/30;
                }
                if($yueshu_quan < 0){
                    $yueshu_quan = 0;
                    $yueshu_zheng = 0;
                    $yueshu_xiao = 0;
                    $yueshu_zhan = 0;
                }else{
                    $yueshu_zheng = floor($yueshu_quan);
                    $yueshu_xiao = $yueshu_quan-$yueshu_zheng;

                    if($yueshu_xiao > 0.3){
                        $yueshu_zhan = $yueshu_zheng+1;
                    }else{
//                        $yueshu_zhan = round($yueshu_quan, 2);
                        $yueshu_zhan = $yueshu_quan;
                    }
                }


                $daihuanben = $daihuanben - $huanArr[$k]['kouben'];
                $lixifee = round($daihuanben*$yueshu_zhan * $jieinfo['yuerate'] / 100, 2);

                $data['yueshu_zhan'] += $yueshu_zhan;
                $data['yinghuanlifee'] += $lixifee;
            }
        }


        $data['yinghuanallfee'] = $data['yinghuanbenfee']+$data['yinghuanlifee'];
        $data['yihuanallfee'] = $data['yihuanbenfee']+$data['yihuanlifee'];

        $data['daihuanbenfee'] = $data['yinghuanbenfee'] - $data['yihuanbenfee'];
        $data['daihuanlifee'] = $data['yinghuanlifee'] - $data['yihuanlifee']-$data['lixiyoufee'];
        $data['daihuanallfee'] = $data['daihuanbenfee']+$data['daihuanlifee'];





        $data['yinghuanbenfee'] = round($data['yinghuanbenfee'], 2);
        $data['yinghuanlifee'] = round($data['yinghuanlifee'], 2);
        $data['yinghuanallfee'] = round($data['yinghuanallfee'], 2);

        $data['yihuanbenfee'] = round($data['yihuanbenfee'], 2);
        $data['yihuanlifee'] = round($data['yihuanlifee'], 2);
        $data['yihuanallfee'] = round($data['yihuanallfee'], 2);
        $data['lixiyoufee'] = round($data['lixiyoufee'], 2);

        $data['daihuanbenfee'] = round($data['daihuanbenfee'], 2);
        $data['daihuanlifee'] = round($data['daihuanlifee'], 2);
        $data['daihuanallfee'] = round($data['daihuanallfee'], 2);

        $data['statename'] = '未还款';
        if($data['yihuanallfee'] <= 0){
            $data['statename'] = '未还款';
        }else{
            if($data['daihuanallfee'] <= 0){
                $data['statename'] = '已还款';
            }else{
                $data['statename'] = '部分还款';
            }
        }

        //天数：借款状态为已还款的话，最近还款日期-放款日期+1；否则就是当前日期-放款日期+1
        $data['tianshu'] = 0;
        if($data['statename'] == '已还款'){
            $data['tianshu'] = ( strtotime($data['zuijindt']) - strtotime($data['daodt']) )/86400+1;
        }else{
            $data['tianshu'] = ( strtotime(date("Y-m-d 00:00:00")) - strtotime($data['daodt']) )/86400+1;
        }

        //放款合计金额

        $data['fangfee'] = HouTiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
            ->where(['jieid'=>$data['jieid']])->sum('money');
        $data['daifee'] = round($jieinfo['money'] - $data['fangfee'], 2);

        return $data;
    }


    public function get_jieshuju($data, $daodt=''){
        //这里的 （ 已还本金 已还利息 已还合计 利息优惠  待还本金 待还利息 待还合计）
        //都是借款收条中的数据合计
        $data['yueshu_zhan'] = 0;
        $data['yinghuanbenfee'] = 0;
        $data['yinghuanlifee'] = 0;
        $data['yinghuanallfee'] = 0;

        $data['yihuanbenfee'] = 0;
        $data['yihuanlifee'] = 0;
        $data['yihuanallfee'] = 0;

        $data['lixiyoufee'] = 0;

        $data['daihuanbenfee'] = 0;
        $data['daihuanlifee'] = 0;
        $data['daihuanallfee'] = 0;

        $tiaoArr = HouTiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
            ->where(['jieid'=>$data['id']])->select()->toArray();

        foreach ($tiaoArr as $vvv){
            $vvv = $this->get_tiaoinfo($vvv, $daodt);
            $data['yueshu_zhan'] += $vvv['yueshu_zhan'];
            $data['yinghuanbenfee'] += $vvv['yinghuanbenfee'];
            $data['yinghuanlifee'] += $vvv['yinghuanlifee'];
            $data['yinghuanallfee'] += $vvv['yinghuanallfee'];

            $data['yihuanbenfee'] += $vvv['yihuanbenfee'];
            $data['yihuanlifee'] += $vvv['yihuanlifee'];
            $data['yihuanallfee'] += $vvv['yihuanallfee'];

            $data['lixiyoufee'] += $vvv['lixiyoufee'];

            $data['daihuanbenfee'] += $vvv['daihuanbenfee'];
            $data['daihuanlifee'] += $vvv['daihuanlifee'];
            $data['daihuanallfee'] += $vvv['daihuanallfee'];
        }


        $data['yueshu_zhan'] = round($data['yueshu_zhan'], 2);
        $data['yinghuanbenfee'] = round($data['yinghuanbenfee'], 2);
        $data['yinghuanlifee'] = round($data['yinghuanlifee'], 2);
        $data['yinghuanallfee'] = round($data['yinghuanallfee'], 2);
        $data['yihuanbenfee'] = round($data['yihuanbenfee'], 2);
        $data['yihuanlifee'] = round($data['yihuanlifee'], 2);
        $data['yihuanallfee'] = round($data['yihuanallfee'], 2);
        $data['lixiyoufee'] = round($data['lixiyoufee'], 2);
        $data['daihuanbenfee'] = round($data['daihuanbenfee'], 2);
        $data['daihuanlifee'] = round($data['daihuanlifee'], 2);
        $data['daihuanallfee'] = round($data['daihuanallfee'], 2);



        $data['statename'] = '未还款';
        if($data['yihuanallfee'] <= 0){
            $data['statename'] = '未还款';
        }else{
            if($data['daihuanallfee'] <= 0){
                $data['statename'] = '已还款';
            }else{
                $data['statename'] = '部分还款';
            }
        }

        //放款合计金额
        $data['fangfee'] = HouTiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
            ->where(['jieid'=>$data['id']])->sum('money');
        $data['daifee'] = round($data['money'] - $data['fangfee'], 2);

        return $data;
    }





}
